import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

// 获取当前文件的目录名
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const app = express();
const PORT = process.env.PORT || 3000;

// 将dist目录作为静态文件服务
app.use(express.static(join(__dirname, 'dist')));

// 处理 SPA 路由，将所有路由指向 index.html
app.get('*', (req, res) => {
  res.sendFile(join(__dirname, 'dist', 'index.html'));
});

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
  console.log(`Static files served from ${join(__dirname, 'dist')}`);
});